//
//  JSKitManager+attrString.m
//  NiubilityProject
//
//  Created by Apple on 2018/5/14.
//  Copyright © 2018年 Apple. All rights reserved.
//

#import "JSKitManager+attrString.h"

@implementation JSKitManager (attrString)


- (js_changeAttributed)attributed{
    __weak typeof(self)weakSelf = self;
    return ^JSKitManager *(NSDictionary *attriDic, NSRange range){
        NSAssert(!attriDic, @"NSDictionary is nil");
        NSInteger lon = range.location + range.length;
        NSAssert(lon>weakSelf.js_AttriStr.length, @"超过字符串长度");
        [weakSelf check];
        [weakSelf.js_AttriStr addAttributes:attriDic range:range];
        return weakSelf;
    };
}
- (js_attributedCorlorRange)frontColorOfRange{
    __weak typeof(self)weakSelf = self;
    return ^JSKitManager *(UIColor *color, NSRange range){
        
        return weakSelf.attributed(@{NSForegroundColorAttributeName:color}, range);
    };
    
}
- (js_attributedCorlorRange)backColorOfRange{
    __weak typeof(self)weakSelf = self;
    return ^JSKitManager *(UIColor *color, NSRange range){
        
        return weakSelf.attributed(@{NSBackgroundColorAttributeName:color}, range);;
    };
}
- (js_attributedFontRange)fontOfRange{
    __weak typeof(self)weakSelf = self;
    return ^JSKitManager *(UIFont *font, NSRange range){
        
        return weakSelf.attributed(@{NSFontAttributeName:font}, range);
    };
}
- (js_attributedObliquenessRange)obliquenessOfRange{
    __weak typeof(self)weakSelf = self;
    return ^JSKitManager *(NSNumber *number, NSRange range){
        
        return weakSelf.attributed(@{NSObliquenessAttributeName:number}, range);
    };
}

- (js_attributedVerticalGlyphRange)verricaRange{
    
    __weak typeof(self)weakSelf = self;
    return ^JSKitManager *(NSNumber *number, NSRange range){

        return weakSelf.attributed(@{NSVerticalGlyphFormAttributeName:number}, range);
    };
}
- (js_changeWritingDirection)writingDirection{
    __weak typeof(self)weakSelf = self;
    return ^JSKitManager *(NSWritingDirection style, NSRange range){
        
        return weakSelf.attributed(@{NSWritingDirectionAttributeName:@(style)}, range);
    };
}

- (js_changeUnderline)underLineOfRange{
    __weak typeof(self)weakSelf = self;
    return ^JSKitManager *(NSUnderlineStyle style, UIColor *color, NSRange range){
        
        return weakSelf.attributed(@{NSUnderlineStyleAttributeName:@(style),NSUnderlineColorAttributeName:color}, range);
    };
}
- (js_changeUnderlineAtAll)underLineAtAll{
    __weak typeof(self)weakSelf = self;
    return ^JSKitManager *(NSUnderlineStyle style, UIColor *color){
        
        return weakSelf.attributed(@{NSUnderlineStyleAttributeName:@(style),NSUnderlineColorAttributeName:color}, NSMakeRange(0, weakSelf.js_AttriStr.length));
    };
}
- (js_changeKern)kernOfRange{
    __weak typeof(self)weakSelf = self;
    return ^JSKitManager *(NSNumber *value, NSRange range){
        
        return weakSelf.attributed(@{NSKernAttributeName:value}, range);
    };
}
-(js_changeExpansion)expansionOfRange{
    __weak typeof(self)weakSelf = self;
    return ^JSKitManager *(NSNumber *value, NSRange range){
        
        return weakSelf.attributed(@{NSExpansionAttributeName:value}, range);
    };
}
- (js_changeShadow)shadowOfRange{
    __weak typeof(self)weakSelf = self;
    return ^JSKitManager *(NSShadow *shadow, NSRange range){
        
        return weakSelf.attributed(@{NSShadowAttributeName:shadow}, range);
    };
}
- (js_changeStrokeColor)strokeColor{
    __weak typeof(self)weakSelf = self;
    return ^JSKitManager *(UIColor *strokeColor,NSNumber *strokeWidth, NSRange range){
        
        return weakSelf.attributed(@{NSStrokeColorAttributeName:strokeColor,NSStrokeWidthAttributeName:strokeWidth}, range);
    };
}
- (js_appendAttachText)appendAttchText{
    __weak typeof(self)weakSelf = self;
    return ^JSKitManager *(UIView *atteach, NSInteger atIndex){
        NSAssert(atIndex > weakSelf.js_AttriStr.length, @"位置越界");
        NSAssert(!atteach, @"对象为 nil");
        NSMutableAttributedString *attachText = [NSMutableAttributedString attachmentStringWithContent:atteach contentMode:UIViewContentModeCenter attachmentSize:atteach.size alignToFont:weakSelf.js_AttriStr.font alignment:YYTextVerticalAlignmentCenter];
        [weakSelf.js_AttriStr insertAttributedString:attachText atIndex:atIndex];
        return weakSelf;
    };
}
-(CGSize)contenSizeFormWidth:(CGFloat)width{
    return [@"" boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
                             options:NSStringDrawingUsesLineFragmentOrigin
                          attributes:self.js_AttriStr.attributes
                             context:nil].size;//计算文字大小
}
-(void)check{
    
}
@end
